import logging as logger
from sqlmodel import SQLModel
from sqlalchemy.ext.asyncio import create_async_engine
from sqlmodel.ext.asyncio.session import AsyncSession
from sqlalchemy.orm import sessionmaker
# from .config import settings

url:str = "mysql+aiomysql://test:test@localhost:3306/db_dingdan"
async_engine = create_async_engine(url,echo=True,future=True)
async def init_db(mode:bool=False):
    from models.user_model import User
    from models.driver_model import Driver
    from models.order_initial_model import OrderInitial
    from models.order_processed_model import OrderProcessed
    if mode:
        async with async_engine.begin() as conn:
            await conn.run_sync(SQLModel.metadata.drop_all)
            logger.info("数据库已删除")
            await conn.run_sync(SQLModel.metadata.create_all)
            logger.info("数据库已创建")
    else:
        logger.info("数据库已存在")

sessionLocal = sessionmaker(async_engine, 
                            class_=AsyncSession, 
                            autoflush=False,
                            autocommit=False,
                            expire_on_commit=False)